<?php


namespace App\Services\Admin;


use App\Models\WtTask;

class TaskService
{
    public function __construct()
    {

    }

    /**
     * 获取单月产品信息
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTaskInfo($params)
    {
        $where = array();
        $where['month_date'] = ['$gt' => 0];
        $pageSize = $params['pageSize'];
        $page = $params['page'];
        $skip = $pageSize * ($page - 1);

        $tasks = WtTask::select("*")->where($where)->limit($pageSize)->skip($skip)->orderBy('month_date', 'desc')->get();
        $total = WtTask::where($where)->count();

        return Success('ok', ['items' => $tasks, 'total' => $total]);
    }

    /**
     * 生成当月待整合数据
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function createTaskInfo()
    {
        $month = date('Y-m', strtotime('-1 month'));

        //判断是否已生成
        $exists = WtTask::where('month',$month)->first();
        if ($exists){
            return Error($month.'-数据已生成！');
        }else{
            $insert[] = [
                'month' => $month,
                'completed' => 'wait',
                'category_completed' => 'wait',
                'order_asin_completed' => 'wait',
                'order_product_completed' => 'wait',
                'update_sj_name_completed' => 'wait',
                'update_sj_name_extra_completed' => 'wait',
                'update_category_completed' => 'wait',
                'low_orders_completed' => [
                    'wt_low_order' => ['loading' => false, 'value' => 0],
                    'update_low' => ['loading' => false, 'value' => 0],
                    'update_current_month' => ['loading' => false, 'value' => 0],
                    'update_last_two_month' => ['loading' => false, 'value' => 0],
                ],
                'sync_asin_system' => 'wait',
                'month_date' => strtotime($month),
                'create_time' => date('Y-m-d H:i:s',time()),
            ];
            $re = WtTask::insert($insert);
            return Success('ok', $re);
        }
    }

}
